<template>
    <div class="list-wrapper">
        <div class="list-top">
            <el-form :inline="true" :model="searchForm">
                <el-form-item label="模拟器名称">
                    <el-input v-model="searchForm.asset_name" placeholder="请输入模拟器名称"></el-input>
                </el-form-item>
                <el-form-item label="型号">
                    <el-input v-model="searchForm.norms" placeholder="请输入型号"></el-input>
                </el-form-item>
                <el-form-item label="模拟器地点">
                    <el-input v-model="searchForm.location" placeholder="请输入模拟器地点"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="search()">搜索</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="">
            <table class="list-table" border="0" cellspacing="0" cellpadding="0">
                <thead>
                <tr>
                    <th>
                        <span class="icon add" @click="OpenSimulator()"></span>
                    </th>
                    <th>序号</th>
                    <th>资产编号</th>
                    <th>模拟器名称</th>
                    <th>规格型号</th>
                    <th>开始使用时间</th>
                    <th>使用部门</th>
                    <th>模拟器地点</th>
                </tr>
                </thead>
                <tbody>
                <tr v-for="(item,index) in list" :key="item.id">
                    <td>
                        <span class="icon del" @click="DelSimulator(item.id)"></span>
                        <span class="icon det" @click="onDetail(item.id)"></span>
                    </td>
                    <td>{{index + 1}}</td>
                    <td>{{item.asset_num}}</td>
                    <td>{{item.asset_name}}</td>
                    <td>{{item.norms}}</td>
                    <td>{{item.use_date}}</td>
                    <td>{{item.department}}</td>
                    <td>{{item.location}}</td>
                </tr>
                </tbody>
            </table>
            <div class="page">
                <div class="block">
                    <el-pagination @current-change="handleCurrentChange" layout="prev, pager, next, jumper"
                                   :total="pageTotal">
                    </el-pagination>
                </div>
            </div>
            <el-dialog title="新建模拟器信息" :visible.sync="pop" size="tiny">
                <el-form :model="SimulatorDatas" :label-width="width" :rules="SimulatorRules" ref="SimulatorRulesName">
                    <el-form-item label="资产编号" prop="asset_num">
                        <el-col :span="span">
                            <el-input v-model="SimulatorDatas.asset_num" size="small" placeholder="请输入资产编号"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="模拟器名称" prop="asset_name">
                        <el-col :span="span">
                            <el-input v-model="SimulatorDatas.asset_name" size="small"
                                      placeholder="请输入模拟器名称"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="规格型号">
                        <el-col :span="span">
                            <el-input v-model="SimulatorDatas.norms" size="small" placeholder="请输入规格型号"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="开始使用时间">
                        <el-col :span="span">
                            <el-date-picker type="date" size="small" placeholder="选择日期"
                                            v-model="SimulatorDatas.use_date" style="width: 100%;"></el-date-picker>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="使用部门">
                        <el-col :span="span">
                            <el-input v-model="SimulatorDatas.department" size="small" placeholder="请输入使用部门"></el-input>
                        </el-col>
                    </el-form-item>
                    <el-form-item label="模拟器地点">
                        <el-col :span="span">
                            <el-input v-model="SimulatorDatas.location" size="small" placeholder="请输入模拟器地点"></el-input>
                        </el-col>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="pop = false">取 消</el-button>
                    <el-button type="primary" @click="ChangeSimulator()">确 定</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>
    const SimulatorData = {
        asset_num: '',
        asset_name: '',
        norms: '',
        use_date: '',
        department: '',
        location: ''
    };
    export default {
        data() {
            return {
                pageIndex: 1,
                pageTotal: 1,
                list: {},
                SimulatorDatas: _g.cloneJson(SimulatorData),

                SimulatorRules: {
                    asset_num: [{required: true, message: '请填写资产编号', trigger: 'blur'}],
                    asset_name: [{required: true, message: '请填写模拟器名称', trigger: 'blur'}]
                },

                searchForm: {
                    asset_name: '',
                    norms: '',
                    location: '',
                },

                width: '200px',
                span: 15,
                pop: false,
                addForm: {
                    id: '',
                    name: '',
                    type: '',
                    startTime: '',
                    department: '',
                    place: '',
                },
            }
        },

        created() {
            this.getSimulatorList();
        },

        methods: {
            search() {
                this.getSimulatorList();
            },

            onDetail(id) {
                router.push('/operate/assets/facilities/simulatorDetail/' + id)
            },

            handleCurrentChange(val) {
                this.pageIndex = val;
                this.getSimulatorList();
            },

            /* 获取数据 */
            getSimulatorList() {
                let data = this.searchForm;
                data.page = this.pageIndex;
                this.apiPost('Operation/simulatorList', data).then(res => {
                    this.list = res.list;
                    this.pageTotal = parseInt(res.total)
                })
            },

            /* 提交数据 */
            ChangeSimulator() {
                this.$refs.SimulatorRulesName.validate((valid) => {
                    if (valid) {
                        this.pop = false;
                        let data = this.SimulatorDatas;
                        this.apiPost('Operation/SimulatorAdd', data).then(res => {
                            let remind = res.error ? 'error' : 'success';
                            _g.toastMsg(remind, res.msg);
                            if (!res.error) {
                                this.getSimulatorList();
                            } else {
                                this.pop = true
                            }
                        })
                    } else {
                        return false;
                    }
                });
            },

            /* 删除 */
            DelSimulator(id) {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let data = {id: id};
                    this.apiPost('Operation/SimulatorDelete', data).then(res => {
                        if (!res.error) {
                            this.$message({
                                type: 'success',
                                message: res.msg
                            });
                        } else {
                            this.$message({
                                type: 'error',
                                message: res.msg
                            });
                        }
                        this.getSimulatorList();
                    });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },

            /* 打开新增窗口 */
            OpenSimulator() {
                this.SimulatorDatas = _g.cloneJson(SimulatorData);
                this.pop = true;
                this.$refs.SimulatorRulesName.resetFields();
            },
        }
    }
</script>

<style scoped>
    .add {
        width: 25px;
        height: 25px;
        background: url(../../../../img/add.png) no-repeat;
        background-size: 100%;
        cursor: pointer;
    }

    .edit {
        background: url(../../../../img/edit.gif) no-repeat;
    }

    .del {
        background: url(../../../../img/del.png) no-repeat;
    }

    .det {
        background: url(../../../../img/detail.png) no-repeat;
        cursor: pointer;
    }
</style>